package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.dao.UserDao;
import com.ytjj.qmyx.admin.mapper.PlatformStrictProfitMapper;
import com.ytjj.qmyx.admin.model.PlatformStrictProfit;
import com.ytjj.qmyx.admin.model.PlatformStrictProfitExample;
import com.ytjj.qmyx.admin.model.request.DynamicExposureRecordRequest;
import com.ytjj.qmyx.admin.model.request.PlatformStrictProfitRequest;
import com.ytjj.qmyx.admin.model.response.DynamicExposureRecordResponse;
import com.ytjj.qmyx.admin.model.response.PlatformStrictProfitResponse;
import com.ytjj.qmyx.admin.service.PlatformStrictProfitService;
import com.ytjj.qmyx.mall.mapper.DynamicExposureRecordMapper;
import com.ytjj.qmyx.mall.mapper.DynamicMapper;
import com.ytjj.qmyx.mall.model.Dynamic;
import com.ytjj.qmyx.mall.model.DynamicExample;
import com.ytjj.qmyx.mall.model.DynamicExposureRecord;
import com.ytjj.qmyx.mall.model.DynamicExposureRecordExample;
import com.ytjj.qmyx.users.mapper.UsersInfoMapper;
import com.ytjj.qmyx.users.model.UsersInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class PlatformStrictProfitServiceImpl implements PlatformStrictProfitService {
    @Resource
    private PlatformStrictProfitMapper yxPlatformStrictProfitMapper;
    @Resource
    private DynamicMapper dynamicMapper;
    @Resource
    private UsersInfoMapper usersInfoMapper;
    @Resource
    private UserDao userDao;
    @Resource
    private DynamicExposureRecordMapper dynamicExposureRecordMapper;

    @Override
    public CommonPage list(String userName, Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PlatformStrictProfitResponse> list = yxPlatformStrictProfitMapper.queryList(userName, userId);
        return CommonPage.restPage(list);
    }

    @Override
    public PlatformStrictProfitResponse detail(Integer userId) {
        List<PlatformStrictProfitResponse> list = yxPlatformStrictProfitMapper.queryList(null, userId);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public Integer save(PlatformStrictProfitRequest request) {
        //判断是否是达人
        UsersInfo usersInfo = usersInfoMapper.selectByPrimaryKey(request.getUserId());
        if (null == usersInfo || null == usersInfo.getIsQuality() || usersInfo.getIsQuality().compareTo(1) != 0){
            throw new ApiException("该用户不是达人");
        }
        List<PlatformStrictProfitResponse> list = yxPlatformStrictProfitMapper.queryList(null, request.getUserId());
        if (CollectionUtils.isEmpty(list)) {
            PlatformStrictProfit profit = new PlatformStrictProfit();
            DynamicExample dynamicExample = new DynamicExample();
            dynamicExample.createCriteria().andUserIdEqualTo(request.getUserId()).andStatusEqualTo((byte) 1);
            List<Dynamic> dynamics = dynamicMapper.selectByExample(dynamicExample);
            profit.setDynamicNum(dynamics.size());
            BeanUtils.copyProperties(request, profit);
            return yxPlatformStrictProfitMapper.insertSelective(profit);
        }
        return yxPlatformStrictProfitMapper.updateByUserId(request);
    }

    @Override
    public CommonPage select(String nickName, Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        //先查用户
        List<DynamicExposureRecordResponse> responses =  userDao.select(nickName,userId);
        responses.forEach(item ->{
            DynamicExposureRecordExample example = new DynamicExposureRecordExample();
            example.createCriteria().andUserIdEqualTo(item.getUserId());
            List<DynamicExposureRecord> dynamicExposureRecords = dynamicExposureRecordMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(dynamicExposureRecords)){
                dynamicExposureRecords.forEach(d ->{
                    if (d.getType() == 1){
                        item.setExposureNumDay(d.getTaskValue());
                        item.setExposureSupport(d.getSupportValue());
                    }else if (d.getType() == 2){
                        item.setBrowseNumDay(d.getTaskValue());
                        item.setBrowseSupport(d.getSupportValue());
                    }
                });
            }
            item.setExposureNumDay(null == item.getExposureNumDay() ? 0 : item.getExposureNumDay());
            item.setExposureSupport(null == item.getExposureSupport() ? 0 : item.getExposureSupport());
            item.setBrowseNumDay(null == item.getBrowseNumDay() ? 0 : item.getBrowseNumDay());
            item.setBrowseSupport(null == item.getBrowseSupport() ? 0 : item.getBrowseSupport());
        });

        return CommonPage.restPage(responses);
    }

    @Override
    public void update(DynamicExposureRecordRequest request) {
        if (!StringUtils.isEmpty(request.getExposureSupport())){
            DynamicExposureRecordExample example = new DynamicExposureRecordExample();
            example.createCriteria().andUserIdEqualTo(request.getUserId()).andTypeEqualTo((byte) 1);
            List<DynamicExposureRecord> dynamicExposureRecords = dynamicExposureRecordMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(dynamicExposureRecords)){
                DynamicExposureRecord dynamicExposureRecord = dynamicExposureRecords.get(0);
                dynamicExposureRecord.setSupportValue(request.getExposureSupport());
                int i = dynamicExposureRecordMapper.updateByPrimaryKey(dynamicExposureRecord);
                if (i == 0){
                    throw new ApiException("保存失败");
                }
            }else {
                DynamicExposureRecord dynamic = new DynamicExposureRecord();
                dynamic.setSupportValue(request.getExposureSupport());
                dynamic.setType((byte) 1);
                dynamic.setUserId(request.getUserId());
                dynamic.setTaskValue(0);
                dynamicExposureRecordMapper.insert(dynamic);
            }
        }
        if (!StringUtils.isEmpty(request.getBrowseSupport())){
            DynamicExposureRecordExample example = new DynamicExposureRecordExample();
            example.createCriteria().andUserIdEqualTo(request.getUserId()).andTypeEqualTo((byte) 2);
            List<DynamicExposureRecord> dynamicExposureRecords = dynamicExposureRecordMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(dynamicExposureRecords)){
                DynamicExposureRecord dynamicExposureRecord = dynamicExposureRecords.get(0);
                dynamicExposureRecord.setSupportValue(request.getBrowseSupport());
                int i = dynamicExposureRecordMapper.updateByPrimaryKey(dynamicExposureRecord);
                if (i == 0){
                    throw new ApiException("保存失败");
                }
            }else {
                DynamicExposureRecord dynamic = new DynamicExposureRecord();
                dynamic.setSupportValue(request.getBrowseSupport());
                dynamic.setType((byte) 2);
                dynamic.setUserId(request.getUserId());
                dynamic.setTaskValue(0);
                dynamicExposureRecordMapper.insert(dynamic);
            }
        }
        PlatformStrictProfitExample platformStrictProfitExample = new PlatformStrictProfitExample();
        platformStrictProfitExample.createCriteria().andUserIdEqualTo(request.getUserId());
        List<PlatformStrictProfit> platformStrictProfits = yxPlatformStrictProfitMapper.selectByExample(platformStrictProfitExample);
        if (!CollectionUtils.isEmpty(platformStrictProfits)){
            PlatformStrictProfit platformStrictProfit = platformStrictProfits.get(0);
            platformStrictProfit.setIncomeRank(request.getIncomeRank());
            yxPlatformStrictProfitMapper.updateByPrimaryKey(platformStrictProfit);//修改收益排行
        }else {
            PlatformStrictProfit platformStrictProfit = new PlatformStrictProfit();
            platformStrictProfit.setIncomeRank(request.getIncomeRank());
            platformStrictProfit.setUserId(request.getUserId());
            yxPlatformStrictProfitMapper.insert(platformStrictProfit);
        }
    }
}
